<template>
  <a-modal
    :open="props.modelVisible"
    title="成员管理"
    :destroyOnClose="true"
    @cancel="handleCancel"
    @ok="handleOk"
    width="710px"
  >
    <div class="container">
      <div style="width: 45%">
        <a-input-group compact style="margin-top: 10px">
          <a-select style="width: 30%" v-model:value="selectType">
            <a-select-option value="名称">名称</a-select-option>
            <a-select-option value="手机号">手机号</a-select-option>
          </a-select>
          <a-input-search
            placeholder="搜索"
            style="width: 70%"
            v-model:value="selectInput"
            @search="findUser"
            enter-button
          />
        </a-input-group>

        <a-tabs v-model:activeKey="selectGroup" :tabBarStyle="{ padding: '0 2%', margin: '0' }">
          <a-tab-pane :key="1" tab="本机构"></a-tab-pane>
          <a-tab-pane :key="2" tab="本帮区"></a-tab-pane>
        </a-tabs>

        <a-list style="height: 73%; overflow-y: auto; margin-top: 10px" :data-source="unSelectData">
          <a-spin :spinning="spinning" v-if="spinning" style="margin: 40% 0" />
          <template #renderItem="{ item }">
            <a-list-item class="item" @click="selectItem(item)" :key="item.userId">
              <span style="width: 80px"> {{ item.userName }}</span>
              <template #actions>
                <CheckOutlined
                  style="color: rgb(102 152 255)"
                  v-show="selectedDate.some((obj) => obj.userId === item.userId)"
                />
              </template>
            </a-list-item>
          </template>
        </a-list>
      </div>

      <a-divider type="vertical" style="height: 100%" />
      <div style="width: 45%">
        <div style="margin-top: 10px">已选择 · {{ selectedDate.length }}</div>
        <a-list style="height: 85%; overflow-y: auto; margin-top: 10px" :data-source="selectedDate">
          <template #renderItem="{ item }">
            <a-list-item class="item">
              <span> {{ item.userName }}</span>
              <a-select
                ref="select"
                v-model:value="item.type"
                style="width: 120px"
                :bordered="false"
                :disabled="item.userId === store.getters.projectCreatorId"
                :showArrow="!(item.userId === store.getters.projectCreatorId)"
              >
                <a-select-option :value="0">负责人</a-select-option>
                <a-select-option :value="1">普通成员</a-select-option>
              </a-select>
              <template #actions>
                <CloseOutlined
                  class="deleteIcon"
                  @click="deleteItem(item.userId)"
                  v-if="item.userId !== store.getters.projectCreatorId"
                />
                <a-tooltip v-else title="创建者不能被修改">
                  <LockOutlined />
                </a-tooltip>
              </template>
            </a-list-item>
          </template>
        </a-list>
      </div>
    </div>
  </a-modal>
</template>
<script lang="ts" setup>
import UserDao from '@/daos/user_dao';
import ProjecrDao from '@/daos/project_dao';
import { useStore } from 'vuex';
import { message } from 'ant-design-vue';
const store = useStore();
const projectDao = new ProjecrDao();
const userDao = new UserDao();

interface Member {
  userId: number;
  userName: string;
  type: number;
}

const props = defineProps({
  modelVisible: {
    type: Boolean,
    default: false
  },
  memberList: {
    type: Array as () => Member[]
  }
});
const emit = defineEmits(['update:modelVisible', 'confirm']);
//筛选
const selectType = ref('名称');
const selectGroup = ref(1);
const selectInput = ref();
//项目类型
const projectType = store.getters.projectType;
//加载开关
const spinning = ref<boolean>(true);
//带选择人员数组
let unSelectData = ref([]);
const selectedDate = ref([]);

const findUser = async () => {
  spinning.value = true;
  if (selectType.value === '账号') {
    const result = await userDao.findUser(selectInput.value, null, null, projectType, null);
    unSelectData.value = result.data.data;
    spinning.value = false;
  } else if (selectType.value === '名称') {
    const result = await userDao.findUser(null, selectInput.value, null, projectType, null);
    unSelectData.value = result.data.data;
    spinning.value = false;
  } else {
    const result = await userDao.findUser(null, null, selectInput.value, projectType, null);
    unSelectData.value = result.data.data;
    spinning.value = false;
  }

  unSelectData.value = unSelectData.value?.filter(
    (item) => !(item.userId === store.getters.projectCreatorId)
  );
};

const selectItem = (newItem) => {
  let index = selectedDate.value.findIndex((item) => item.userId === newItem.userId);
  if (index === -1) {
    newItem.type = 1;
    selectedDate.value.push(newItem);
  } else {
    selectedDate.value.splice(index, 1);
  }
};

const deleteItem = (userId) => {
  let index = selectedDate.value.findIndex((item) => item.userId === userId);
  selectedDate.value.splice(index, 1);
};
const handleCancel = () => {
  emit('update:modelVisible', false);
};
const handleOk = async () => {
  const result = await projectDao.upsertDeleteUsers(store.getters.projectId, selectedDate.value);

  message.success(result.data.data);
  emit('confirm');
  emit('update:modelVisible', false);
};
watch(
  () => props.modelVisible,
  (newValue) => {
    if (newValue == true) {
      selectedDate.value = props.memberList.map((item) => ({ ...item }));
      findUser();
    }
  }
);
</script>
<style lang="scss" scoped>
.container {
  display: flex;
  justify-content: space-around;
  width: 100%;
  height: 440px;
}

.item {
  display: flex;
  justify-content: space-between;
}

.item:hover {
  cursor: pointer;
  background-color: #f5f5f5aa;
}

.deleteIcon:hover {
  color: red;
}
</style>
